package scm.test;

import scm.server.MailServer;
import scm.message.Message;
import scm.message.Cabecera;
import scm.message.Contenido;
import scm.message.MailUser;
import javax.swing.JOptionPane;

/**
 * 
 * @author Herlan
 */
public class frmMensaje extends javax.swing.JFrame {
    private Message mensaje;
//    private MailClient cliente;
    private MailServer server;
    /** Creates new form interfaz */
    public frmMensaje(String Usuario) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.txtSender.setText(Usuario);
//        cliente = new MailClient( this.txtSender, this.txtDestinatario, this.txtCorreo, this.txtMensaje );
        server = new MailServer(this.txtSender, this.txtDestinatario, this.txtCorreo, this.txtMensaje);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtSender = new javax.swing.JTextField();
        txtDestinatario = new javax.swing.JTextField();
        txtMensaje = new javax.swing.JTextField();
        btnEnviar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtCorreo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        btnLimpiar1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mAbrir = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        mSgte = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(null);
        /**
        * @web http://www.jc-mouse.net/
        * @author Mouse
        */

        jLabel1.setText("Sender");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(30, 70, 90, 14);

        jLabel2.setText("Destinatario:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(30, 100, 90, 14);

        jLabel3.setText("Mensaje:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(30, 160, 90, 14);

        txtSender.setEditable(false);
        txtSender.setPreferredSize(new java.awt.Dimension(120, 20));
        jPanel1.add(txtSender);
        txtSender.setBounds(140, 70, 260, 20);

        txtDestinatario.setPreferredSize(new java.awt.Dimension(260, 20));
        jPanel1.add(txtDestinatario);
        txtDestinatario.setBounds(140, 100, 260, 20);

        txtMensaje.setPreferredSize(new java.awt.Dimension(260, 20));
        jPanel1.add(txtMensaje);
        txtMensaje.setBounds(140, 160, 260, 60);

        btnEnviar.setText("Enviar");
        btnEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEnviarActionPerformed(evt);
            }
        });
        jPanel1.add(btnEnviar);
        btnEnviar.setBounds(120, 250, 90, 23);

        btnLimpiar.setText("Nuevo");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });
        jPanel1.add(btnLimpiar);
        btnLimpiar.setBounds(220, 250, 90, 23);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel4.setText("REGISTRAR MENSAJES");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(140, 30, 180, 17);

        txtCorreo.setPreferredSize(new java.awt.Dimension(260, 20));
        jPanel1.add(txtCorreo);
        txtCorreo.setBounds(140, 130, 260, 20);

        jLabel5.setText("Asunto:");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(30, 130, 90, 14);

        btnLimpiar1.setText("Siguiente");
        btnLimpiar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiar1ActionPerformed(evt);
            }
        });
        jPanel1.add(btnLimpiar1);
        btnLimpiar1.setBounds(320, 250, 90, 23);

        jMenu1.setText("Archivo");

        mAbrir.setText("Abrir archivo *.TXT");
        mAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mAbrirActionPerformed(evt);
            }
        });
        jMenu1.add(mAbrir);
        jMenu1.add(jSeparator1);

        jMenuItem3.setText("Salir");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Herramientas");

        mSgte.setText("Siguiente Mensaje");
        mSgte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mSgteActionPerformed(evt);
            }
        });
        jMenu2.add(mSgte);

        jMenuItem1.setText("Cantidad Mensajes");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 455, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mSgteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mSgteActionPerformed
        server.siguiente(txtSender.getText());
    }//GEN-LAST:event_mSgteActionPerformed

    private void btnEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEnviarActionPerformed
        // TODO add your handling code here:
        Contenido contenido = new Contenido(this.txtCorreo.getText(), this.txtMensaje.getText());

        MailUser sender = new MailUser(this.txtSender.getText());
        MailUser destinatario = new MailUser(this.txtDestinatario.getText());
        Cabecera cabecera = new Cabecera(sender, destinatario);

        mensaje = new Message(cabecera, contenido);
        server.post(mensaje);
        limpiar();
    }//GEN-LAST:event_btnEnviarActionPerformed

    private void mAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mAbrirActionPerformed
       String nom = txtSender.getText();
       server.AbrirArchivoMensaje();
       txtSender.setText(nom);
       limpiar();
}//GEN-LAST:event_mAbrirActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        // TODO add your handling code here:
        limpiar();
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(null, "Cantidad de Mensajes : " + server.getCantidadMensaje(txtSender.getText()));
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void btnLimpiar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiar1ActionPerformed
        // TODO add your handling code here:
        server.siguiente(txtSender.getText());
    }//GEN-LAST:event_btnLimpiar1ActionPerformed

    private void limpiar(){
        txtMensaje.setText("");
        txtDestinatario.setText("");
        txtCorreo.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEnviar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnLimpiar1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JMenuItem mAbrir;
    private javax.swing.JMenuItem mSgte;
    private javax.swing.JTextField txtCorreo;
    private javax.swing.JTextField txtDestinatario;
    private javax.swing.JTextField txtMensaje;
    private javax.swing.JTextField txtSender;
    // End of variables declaration//GEN-END:variables

}
